package com.aplink.generic.pojo;

import java.io.Serializable;
import java.util.Arrays;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

public class GenericAttribute implements Serializable, Parcelable {

    private static final long serialVersionUID = -2193013652023221974L;

    public static final Parcelable.Creator<GenericAttribute> CREATOR = new Parcelable.Creator<GenericAttribute>() {

        @Override
        public GenericAttribute createFromParcel(final Parcel source) {
            return new GenericAttribute(source);
        }

        @Override
        public GenericAttribute[] newArray(final int size) {
            return new GenericAttribute[size];
        }
    };

    public static GenericAttribute obtain(final String value,
            final ObjPropInfo objPropInfo) {
        return new GenericAttribute(objPropInfo.getColumnName(), value);
    }

    public static GenericAttribute obtain(final String value,
            final String columnName) {
        return new GenericAttribute(columnName, value);
    }

    private final String mName;

    private String mValue;

    private GenericAttribute(final Parcel parcel) {
        this.mName = parcel.readString();
        this.mValue = parcel.readString();
    }

    public GenericAttribute(final String _name, final String _value) {
        this.mName = _name;
        this.mValue = _value;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public String getName() {
        return this.mName;
    }

    public String getValue() {
        return this.mValue;
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(new Object[] { new String(this.mName),
                new String(this.mValue) });
    }

    public boolean isValid() {
        if (TextUtils.isEmpty(this.mValue)) {
            return false;
        }
        if (this.mValue.equalsIgnoreCase("null")) {
            return false;
        }
        return true;
    }

    public void setValue(final String value) {
        this.mValue = value;
    }

    @Override
    public String toString() {
        return String.format("[%s:%s]", this.mName, this.mValue);
    }

    @Override
    public void writeToParcel(final Parcel dest, final int flags) {
        dest.writeString(this.mName);
        dest.writeString(this.mValue);
    }
}
